package com.cuiyun97.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cuiyun97.constant.AdminConstant;
import com.cuiyun97.constant.DefaultConstant;
import com.cuiyun97.context.BaseContext;
import com.cuiyun97.domain.dto.AdminDTO;
import com.cuiyun97.domain.dto.AdminPageQueryDTO;
import com.cuiyun97.domain.po.Admin;
import com.cuiyun97.domain.vo.AdminVO;
import com.cuiyun97.exception.AdminException;
import com.cuiyun97.mapper.AdminMapper;
import com.cuiyun97.result.PageResult;
import com.cuiyun97.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;

    /**
     * 分页查询
     *
     * @param adminPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(AdminPageQueryDTO adminPageQueryDTO) {

        // Map<String, Integer> map = BaseContext.get();
        // System.out.println(map);

        // 开始分页查询
        PageHelper.startPage(adminPageQueryDTO.getPage(), adminPageQueryDTO.getPageSize());
        Page<Admin> page = adminMapper.pageQuery(adminPageQueryDTO);

        // 总记录, 记录集合
        long total = page.getTotal();
        List<Admin> records = page.getResult();

        // 返回
        List<AdminVO> adminVOList = BeanUtil.copyToList(records, AdminVO.class);
        return new PageResult(total, adminVOList);
    }

    /**
     * 管理员登录
     *
     * @param adminDTO
     * @return
     */
    @Override
    public Admin login(AdminDTO adminDTO) {

        String username = adminDTO.getUsername();
        String password = adminDTO.getPassword();

        // 帐号为空
        if (username.equals("")) {
            throw new AdminException(AdminConstant.ACCOUNT_IS_BANK);
        }

        Admin admin = adminMapper.getByUsername(username);

        // 帐号不存在
        if (admin == null) {
            throw new AdminException(AdminConstant.ACCOUNT_NOT_EXIST);
        }

        // 没有加密
        // password = DigestUtils.md5DigestAsHex(password.getBytes());
        // 密码错误
        if (!password.equals(admin.getPassword())) {
            throw new AdminException(AdminConstant.PASSWORD_ERROR);
        }

        // 状态被禁用
        if (admin.getStatus() == 0) {
            throw new AdminException(AdminConstant.ACCOUNT_DISABLED);
        }

        return admin;
    }

    /**
     * 新增管理员
     *
     * @param adminDTO
     */
    @Override
    public void save(AdminDTO adminDTO) {
        Admin admin = new Admin();

        // 对象属性拷贝
        BeanUtil.copyProperties(adminDTO, admin);

        // 设置状态
        admin.setStatus(DefaultConstant.STATUS_ENABLE);

        // 设置默认密码
        admin.setPassword(DefaultConstant.DEFAULT_PASSWORD);

        // 创建时间和修改时间
        // admin.setCreateTime(LocalDateTime.now());
        // admin.setUpdateTime(LocalDateTime.now());

        // 创建人ID和修改人ID
        // admin.setCreateUser(BaseContext.getAdminId());
        // admin.setUpdateUser(BaseContext.getAdminId());

        // 删除状态
        admin.setDeleted(DefaultConstant.NOT_DELETE);

        // 排序
        admin.setSort(0);

        // 超级管理员
        admin.setIsCyzz(0);

        adminMapper.insert(admin);
    }

    /**
     * 状态修改
     *
     * @param status
     * @param id
     */
    @Override
    public void status(Integer status, Integer id) {
        // Admin admin = new Admin();
        // admin.setId(id);
        // admin.setStatus(status);

        Admin admin = Admin.builder()
                .id(id)
                .status(status)
                .build();

        adminMapper.update(admin);
    }

    /**
     *
     * 根据ID查询管理员
     *
     * @param id
     * @return
     */
    @Override
    public AdminVO getById(Integer id) {
        Admin admin = adminMapper.getById(id);
        // 其实不用设置, 因为VO没有这个字段
        admin.setPassword("******");
        AdminVO adminVO = BeanUtil.copyProperties(admin, AdminVO.class);
        return adminVO;
    }

    /**
     * 更新管理员
     *
     * @param adminDTO
     */
    @Override
    public void update(AdminDTO adminDTO) {
        Admin admin = BeanUtil.copyProperties(adminDTO, Admin.class);

        // 修改时间与操作人
        // admin.setUpdateTime(LocalDateTime.now());
        // admin.setUpdateUser(BaseContext.getAdminId());

        // 更新
        adminMapper.update(admin);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {

        // todo 记录删除日志, 时间, 操作人ID

        adminMapper.delete(ids);
    }
}
